package com.jason.library.regular

import java.util.regex.Pattern

object RegularHelper {

    /**
     * 正则：数字或者是小数
     */
    const val FLOAT_REGULAR = "^(\\d+)(\\.\\d+)?$"

    /**
     * 正则：邮箱
     */
    const val EMAIL_REGULAR =
        "^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$"

    /**
     * 正则：qq号码
     */
    const val QQ_REGULAR = "^[1-9][0-9]{4,}"

    /**
     * 正则：手机号码
     */
    const val PHONE_NUM_REGULAR = "^(13[0-9]|14[5-9]|15[0-3,5-9]|16[2,5,6,7]|17[0-8]|18[0-9]|19[0-3,5-9])\\d{8}$"

    /**
     * 中国移动：China Mobile
     *13[4-9],147,148,15[0-2,7-9],165,170[3,5,6],172,178,18[2-4,7-8],19[5,7,8]
     */
    const val CM_NUM = "^((13[4-9])|(14[7-8])|(15[0-2,7-9])|(165)|(178)|(18[2-4,7-8])|(19[5,7,8]))\\d{8}|(170[3,5,6])\\d{7}$";

    /**
     * 中国联通：China Unicom
     * 130,131,132,145,146,155,156,166,167,170[4,7,8,9],171,175,176,185,186,196
     */
    const val CU_NUM = "^((13[0-2])|(14[5,6])|(15[5-6])|(16[6-7])|(17[1,5,6])|(18[5,6])|(196))\\d{8}|(170[4,7-9])\\d{7}$";

    /**
     * 中国电信：China Telecom
     * 133,149,153,162,170[0,1,2],173,174[0-5],177,180,181,189,19[0,1,3,9]
     */
    const val CT_NUM = "^((133)|(149)|(153)|(162)|(17[3,7])|(18[0,1,9])|(19[0,1,3,9]))\\d{8}|((170[0-2])|(174[0-5]))\\d{7}$";

    /**
     * 中国广电：China Broadcasting Network
     * 192
     */
    const val CBN_NUM = "^((192))\\d{8}$";

    /**
     * 正则：汉字
     */
    const val HAN_ZI_REGULAR =
        "((?![\\u3000-\\u303F])[\\u2E80-\\uFE4F]|\\·)*(?![\\u3000-\\u303F])[\\u2E80-\\uFE4F](\\·)*$"


    /**
     * 判断是否是正确邮件邮件格式
     */
    fun isEmailFormat(email: String) = matchRegular(email, EMAIL_REGULAR)

    /**
     * 判断是数字还是浮点数
     */
    fun isDigitOrFloat(text: String) = matchRegular(text, FLOAT_REGULAR)

    /**
     * 判断是否是正确的QQ格式
     */
    fun isQQFormat(text: String) = matchRegular(text, QQ_REGULAR)

    /**
     * 判断是否是正确的手机号码格式
     */
    fun isPhoneNumberFormat(text: String) = matchRegular(text, PHONE_NUM_REGULAR)

    /**
     * 判断是否是正确的汉字
     */
    fun isHanziFormat(text: String) = matchRegular(text, HAN_ZI_REGULAR)

    /**
     * 字符串正则匹配
     */
    private fun matchRegular(originString: String, regular: String): Boolean {
        val pattern = Pattern.compile(regular)
        val matcher = pattern.matcher(originString)
        return matcher.matches()
    }
}